package com.pimee.service.admin.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageInfo;
import com.pimee.common.util.DateUtil;
import com.pimee.mapper.ext.ExtSysPostMapper;
import com.pimee.model.SysPost;
import com.pimee.service.BaseService;
import com.pimee.service.admin.ISysPostService;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class SysPostService extends BaseService<SysPost> implements ISysPostService {

	@Autowired
	private ExtSysPostMapper extSysPostMapper;

	@Override
	public List<SysPost> selectPostsByUserId(Long userId) {
		List<SysPost> userPosts = extSysPostMapper.selectPostsByUserId(userId);
		List<SysPost> posts = this.listAll();
		for (SysPost post : posts) {
			for (SysPost userRole : userPosts) {
				if (post.getPostId().longValue() == userRole.getPostId().longValue()) {
					post.setFlag(true);
					break;
				}
			}
		}
		return posts;
	}

	@Override
	public PageInfo<SysPost> pageList(Map<String, Object> params) {
		log.info("===> 岗位列表分页查询...");
		startPage(params);// 设置分页
		List<SysPost> list = extSysPostMapper.pageList(params);
		return new PageInfo<>(list);
	}

	@Override
	public void insertPost(SysPost param) {
		Date now = DateUtil.now();
		param.setCreateTime(now);
		param.setUpdateTime(now);
		this.saveNotNull(param);
	}

	@Override
	public void updatePost(SysPost param) {
		param.setUpdateTime(DateUtil.now());
		this.updateNotNull(param);
	}

}
